layui.use(['form','element','jquery','layer','laydate'], function(){
	var $=layui.jquery,form=layui.form,laydate = layui.laydate;
 	form.on("submit(addNews)",function(data){
		var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
		var zt=null;
		//职位ptIid
		var ptIid=$("div[name=ptIname] select").next().find("dl .layui-this").attr("lay-value");
		$.ajax({
			type:"post",
			url:"../recruitmentManagementAction!ptIidShowRecruitinfo",
			async:false,
			dataType:'json',
			data:{ptIid:ptIid},
			success:function show(z){
				layer.close(index);
				if(z)
				{
					zt=0;
				}else{
					zt=1;
				}
			},error:function(){
 				location.href="404.jsp";
 			}
		});
		
		if(zt==0){
			var ww=layer.confirm('该职位需求已存在！是否跳转到管理页面进行查看?', {icon: 3, title:'提示'}, function(index){
				//do something
				parent.$("a[data-url='recruit.jsp']").trigger("click");
				layer.close(ww);
			});
		}
		if(zt==1) {
			//数量
			var riicount=$("div[name=riicount] dl .layui-this").attr("lay-value");
			//开始时间
			var riibeginTime=$(".newsTime").val();//newsEndTime
			//结束时间
			var riiendTime=$(".newsEndTime").val();
			if(riiendTime==undefined){
				riiendTime=null;
			}
			//任职条件
			var list = $("div[name='txt'] textarea");
			var rirequirement="";
			for(var i=0;i<list.length;i++)
			{
				rirequirement+= $("div[name='txt'] textarea").eq(i).val();
				if(i<list.length-1)
				{
					rirequirement+=";";
				}
			}
			//地址
			var riaddress= $(".newsAddress").val();
			$.ajax({
				type:"post",
				url:"../recruitmentManagementAction!addrecruitinfo",
				async:false,
				dataType:'json',
				data:{riaddress:riaddress,rirequirement:rirequirement,riicount:riicount,riibeginTime:riibeginTime,riiendTime:riiendTime,ptIid:ptIid},
				success:function show(z){
					if(z)
					{
						layer.alert('发布成功', {icon: 1});
					}else{
						layer.msg('发布失败', {icon: 5});
					}
				},error:function(){
	 				location.href="404.jsp";
	 			}
			});
		}
 		return false;//阻止表单提交
 	})
	//核心方法 
	function timeAdd(){ 
		lay('.datetime').each(function() { 
		laydate.render({ elem : this, trigger : 'click'}); 
		});
	}
	$(function(){
		//加载职位下拉ajax
		$.ajax({
			type:"post",
			url:"../recruitmentManagementAction!positioninfoShowAll",
			async:false,
			dataType:'json',
			data:{},
			success:function show(z){
				$("div[name=ptIname] select").html("");
				for(var i=0;i<z.length;i++)
				{
					$("div[name=ptIname] select").append("<option value="+z[i].ptIid+">"+z[i].ptIname+"</option>");
				}
				//alert("ss")
				form.render('select');
			}
		});
		
		//累加招聘人数
		$("div[name=riicount] select").html("");
		for(var i=1;i<=100;i++)
		{
			$("div[name=riicount] select").append("<option value="+i+">"+i+"</option>");
		}
		
		$("#add").click(function(){
			var num=$(".newsEndTime").length;
			if(num>=1)
			{
				return;
			}
			var t=$("<div class='layui-input-inline'><input type='text' class='layui-input datetime newsEndTime' lay-verify='date'></div>");
			$(this).before(t);
			form.render(); 
	        timeAdd();
			$("#delete").click(function(){
				t.remove();
			});
			t.trigger("focus");
			createdate();
		});
		function createdate(){
			laydate.render({
				elem: 'input[name=date]:eq(1)' //指定元素
				});
		}
		
		$("#addtj").click(function(){
			var num=$("span[name=num]").length;
			var txt=$("<div name='txt'><header class='larry-personal-tit'><i class='layui-icon'></i>条件<span name='num'>"+(num+1)+"</span></header></div>");
			var t=$("<textarea placeholder='请输入要求' class='layui-textarea newCondition'></textarea>");
			txt.append(t);
			$(this).before(txt);
			t.trigger("focus");
		});
		$("#deletetj").click(function(){
			if($("span[name=num]").length>1){
				var txt=$("div[name=txt]").last();
				txt.remove();
				txt.trigger("focus");
			}
		});
		form.on('select(ptInamelist)', function(data){
			var ptIid=data.value; //得到被选中的值
			$.ajax({
				type:"post",
				url:"../recruitmentManagementAction!positioninfoShowId",
				async:false,
				dataType:'json',
				data:{ptIid:ptIid},
				success:function show(z){
					$("#gwzz").html("");
					var str0=(z.ptIrequire).split(";");
					
					for(var i=0;i<str0.length;i++)
					{//<i class="layui-icon "></i>要求<span>1.</span>专科及以上学历，汽车工程、机械制造、机械设计等相关专业毕业；<br/>
						$("#gwzz").append("<i class='layui-icon '></i>要求<span>"+(i+1)+".</span>"+str0[i]+"；");
						if(i<str0.length-1){
							$("#gwzz").append("<br/>");
						}
					}
				},error:function(){
	 				location.href="404.jsp";
	 			}
			});
			form.render('select');
		});
		
		$("div[name=ptIname] select").next().find('dl dd:eq(0)').click();
		timeAdd();
	});
});